﻿
var showMessage = function () {
    $("input").hover(function () {
        $(this).css("border-color", "darkseagreen");
        $("#lbl1").css("color", "green");
    }, function () {
        $(this).css("border-color", "");
    })
    //鼠标移开时验证
    $(".yui-text").blur(function () {
        if (typeof (checkRequireData) != 'undefined') {
            if ($(this).attr("requireType") == 1) {
                checkRequireData($(this));
            }
        }
    })
    $("[requireType=1]:input").each(function () {
        var $required = $("<span style='color:Red; font-weight:bold; font-size:larger'>*</span>");
        $(this).parent().append($required);
    })

    $(".yui-mail").blur(function () {
        if (typeof (checkEmail) != 'undefined') {
            checkEmail($(this), $(this).attr("value"));
        }
    })

}
var i = 0;
var name = "";
var checkInputData = function () {
    i = 0;
    name = "";
    $("[requireType=1]").each(function () {
        var names = checkRequireData($(this));
        if (names.toString().length > 2) {
            if (name == "")
                name = names;
            else {
                name += ',' + names;
            }
        }
    });
    if ($("[requireType=1]").length == 0 || $("[requireType=1]").length == i) {
        return 1;
    }
    else {
        alert('必填项 [' + name + '] 不能为空!');
        return 0;

    }
}
//必填项验证,调用时只需要传入字段名即可
var checkRequireData = function (obj) {
    var field = $("#" + obj.attr("id"));
    var td = $('#td' + obj.attr("id"));
    var msg = "";
    if ($.trim(field.val()) == "") {
        var names = obj.attr("title").toString();
        field.css("background-color", "mistyrose");
        $('#td' + obj.attr("id") + ' font').show();
        $('#td' + obj.attr("id") + ' img').show();
        if (td.html() != null && td.html().indexOf("<IMG") == -1) {

            msg += "<img src='../Images/icon/smallFail.gif' height='20px' width='20px' style='vertical-align:bottom'"
            //td.prepend("<font color='red'>*</font>");
            td.append(msg);

        }
        return names;
    }
    else {
        field.css("background-color", "");
        if (td.html() != null && td.html().indexOf("<IMG") != -1) {
            $('#td' + obj.attr("id") + ' font').hide();
            $('#td' + obj.attr("id") + ' img').hide();
        }
        i++;
    }
    $("#" + obj.attr("id")).focus(
        function () {
            field.css("background-color", "");
            // if (td.html().indexOf("</font>") != -1) {
            $('#td' + obj.attr("id") + ' font').hide();
            $('#td' + obj.attr("id") + ' img').hide();
            // }
        }
    );
    return i;
}


//进行邮箱验证
function checkEmail(obj, email) {
    var emailRegExp = new RegExp("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");
    if (!emailRegExp.test(email) || email.indexOf('.') == -1) {
        obj.css("background-color", "mistyrose");
        return false;
    } else {
        obj.css("background-color", "");
        return true;
    }
} 